package com.ray.controller;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ray.bean.CompanyMaterialCountBean;
import com.ray.bean.DatatablesViewPageBean;
import com.ray.bean.MaterialPositionBean;
import com.ray.service.CompanyMaterialCountService;
import com.ray.service.PurchaseOrderService;
import com.ray.util.ExcelExportUtil;
import com.ray.util.RequestUtil;
import com.ray.util.ResponseUtil;

/**
 * 公司库存表
* <p>Title: CompanyMaterialCountController</p>  
* <p>Description: </p>  
* @author 李斌  
* @date 2018年9月17日
 */
@Controller
@RequestMapping("/companyMaterialCount")
public class CompanyMaterialCountController {
	
	@Autowired
	CompanyMaterialCountService companyMaterialCountService;
	@Autowired
	PurchaseOrderService purchaseOrderService;
	
	/**
	 * 获取公司库存
	 * @param request
	 * @return
	 */
	@RequestMapping("/getCompanyMaterialCountList")
	@ResponseBody
	public DatatablesViewPageBean<CompanyMaterialCountBean> getCompanyMaterialCountList(HttpServletRequest request){
		Map<String,Object> map = RequestUtil.getRequestAllParam(request);
		return  companyMaterialCountService.getCompanyMaterialCountListByPage(map);
	}
	
	@ResponseBody
	@RequestMapping(value="/exportExcelFileByCompanyMaterial",method=RequestMethod.GET)
	public void exportExcelFileByCompanyMaterial(HttpServletRequest request,HttpServletResponse response) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, IOException {
		 Map<String, Object> map = RequestUtil.getRequestAllParam(request);
		 List<CompanyMaterialCountBean> companyMaterialCountBeans = companyMaterialCountService.getExportExcelFileByCompanyMaterial(map);
		 String tempFolder=request.getServletContext().getRealPath("/")+"tempExcelFolder";
		 List<MaterialPositionBean> materialPositionList = companyMaterialCountService.getMaterialPositionList(map);
		 for(CompanyMaterialCountBean companyMaterialCountBean:companyMaterialCountBeans) {
		     if(materialPositionList !=null && materialPositionList.size()>0) {
				//设置物料仓位
				List<String> positionList = materialPositionList.stream().filter(po->po.getMaterialCode().equals(companyMaterialCountBean.getMaterialCode())).map(MaterialPositionBean::getPosition).collect(Collectors.toList());
				companyMaterialCountBean.setPositionList(positionList);
			 }
			 companyMaterialCountBean.setRowIndexStr();
			 companyMaterialCountBean.setExportCount(companyMaterialCountBean.getCount()+" "+(companyMaterialCountBean.getUnit()==null?"":companyMaterialCountBean.getUnit()));
			 companyMaterialCountBean.setExportProductionUnitCount((companyMaterialCountBean.getProductionUnitCount()==null?0:companyMaterialCountBean.getProductionUnitCount())+" 付");
		 }   
		 File excelFile =null;
		 if(null!=map.get("warehouseType") && "1".equals(map.get("warehouseType").toString())) {
			 String [] fields= {"orderId","companyOrderId","factoryNo","customerNo","rowIndexStr","colorDescription","exportCount","positionList"};
			 String [] heads = {"系统编号","公司订单编号","工厂型号","客户型号","行号","颜色描述","库存数","仓位"};
			 excelFile=ExcelExportUtil.createExcelFile(companyMaterialCountBeans,tempFolder,heads,fields);
		 }else {
			 String [] fields  = {"providerName","materialName","model","color","materialDeptName","defaultWarehouse","exportCount","materialUseCount","unitPrice","exportProductionUnitCount","sumPrice","positionList"};
			 String [] heads = {"供应商","物料名称","规格","编号","物料部门","仓库","库存数","占用数","物料单价","生产库存数","总价","仓位"};
			 excelFile=ExcelExportUtil.createExcelFile(companyMaterialCountBeans,tempFolder,heads,fields);
		 }
		 ResponseUtil.downloadfile(response, excelFile, "库存记录.xlsx", true);
	}
	
	
	@ResponseBody
	@RequestMapping(value="/exportOrderMaterialExcelFile",method=RequestMethod.GET)
	public void exportOrderMaterialExcelFile(HttpServletRequest request,HttpServletResponse response) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, IOException {
		Map<String, Object> map = RequestUtil.getRequestAllParam(request);
		map.put("start", null);
		map.put("length", null);
		DatatablesViewPageBean<CompanyMaterialCountBean> xx=companyMaterialCountService.getOrderMaterialStockCount(map);
		List<CompanyMaterialCountBean> companyMaterialCountBeans = xx.getReturnData();
		for(CompanyMaterialCountBean companyMaterialCountBean :companyMaterialCountBeans) {
			if(companyMaterialCountBean.getStockCount()<=0) {
				companyMaterialCountBean.setStockCount(0);
			}
			companyMaterialCountBean.setRowIndexStr();
		}
		String [] fields  = {"orderId","companyOrderId","factoryNo","customerNo","rowIndexStr","providerName","materialName","color","colorDescription","model","providerNo","feedCount","purchaseCount","receiveCount","inCount","outCount","stockCount"};
		String [] heads = {"系统编号","订单编号","工厂型号","客户型号","行号","供应商","物料名称","色号","色号描述","规格","供方编号","投料数","采购数","已领数","入库数","出库数","库存数"};
		String tempFolder=request.getServletContext().getRealPath("/")+"tempExcelFolder";
		File excelFile=ExcelExportUtil.createExcelFile(companyMaterialCountBeans,tempFolder,heads,fields);
		ResponseUtil.downloadfile(response, excelFile, "订单物料库存记录.xlsx", true);
	}
	
	/**
	 * 通过出入库记录获取库存记录
	* @date 2018年9月20日 上午8:08:02 
	* @author 李斌
	* @return Map<String,Object> 
	* @throws
	 */
	@RequestMapping("/getCompanyMaterialStackByWarehouseInOut")
	@ResponseBody
	public Map<String,Object> getCompanyMaterialStackByWarehouseInOut(HttpServletRequest request){
		Map<String,Object> map = RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(companyMaterialCountService.getCompanyMaterialStackByWarehouseInOut(map));
	}
	
	/**
	 * 获取库存物料的总额
	* @date 2018年9月21日 上午11:26:54 
	* @author 李斌
	* @return Map<String,Object> 
	* @throws
	 */
	@RequestMapping("/getCompanyMaterialCountPrice")
	@ResponseBody
	public Map<String,Object> getCompanyMaterialCountPrice(HttpServletRequest request){
		Map<String,Object> map = RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(companyMaterialCountService.getCompanyMaterialCountPrice(map));
	}
	
	/**
	 * 获取某物的占用数
	 * @param request
	 * @return
	 */
	@RequestMapping("/getAvailableCountByFeedData")
	@ResponseBody
	public Map<String,Object> getAvailableCountByFeedData(HttpServletRequest request){
		Map<String,Object> map = RequestUtil.getRequestAllParam(request);
		Assert.notNull(map.get("materialCode"),"物料代码为空");
		return ResponseUtil.ResponseResult(purchaseOrderService.getMaterialUseCountByFeed(map.get("materialCode").toString(), map.get("loginCompany").toString()));
	}
	
	
	/**
	 * 获取订单物料库存
	 * @param request
	 * @return
	 */
	@RequestMapping("/getOrderMaterialStockCount")
	@ResponseBody
	public Map getOrderMaterialStockCount(HttpServletRequest request) {
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(companyMaterialCountService.getOrderMaterialStockCount(map));
	}
	
	
	
	/**
	 * 设置成品单价
	 * @param request
	 * jsonArray
	 * orderId 订单编号  rowIndex 行号  unitPrice 单价
	 * @return
	 */
	@RequestMapping("/setFinishUnitPrice")
	@ResponseBody
	public Map setFinishUnitPrice(HttpServletRequest request) {
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(companyMaterialCountService.setFinishUnitPrice(map));
	}
	
}
